[ETR #14] Build Your Staging Tables Faster/Safer


Extract. Transform. Read.

A newsletter from Pipeline: Your Data Engineering Resource

Presented by Basejump AI

Use natural language prompts to chat with your database in Basejump’s intuitive interface, or embed it directly in your application. Book your demo here.

Hi past, present or future data professional!

One thing that makes my work day easier is when I’m Google-ing (as all software developers do) a problem and I come across the holy grail of solutions: A one-line implementation.

Like anything, however, a one-liner that is too complex can become a bad thing. Think: Chained Pandas expressions that become unreadable. Or cramming a multi-line query inside of a BigQuery client method.

My favorite one line (at least in recent memory) is a clause used with SQL’s ALTER TABLE statement: RENAME TO. You may find renaming a table as compelling as schema creation. But this simple clause can be especially useful in lieu of a more dangerous phrase: CREATE OR REPLACE.

The RENAME command allows you to rename a table without having to completely recreate its contents–and risk a SQL statement failing and losing some or all of your data.

Specifically, I use RENAME TO when I want to convert a copy table with some change, like an updated schema, to a production table. I do so using these steps:

  • Create/backfill a staging table I’ll ultimately convert to prod
  • Use ALTER TABLE `dataset.production_table` RENAME TO `production_table_original`
  • Use ALTER TABLE `dataset.staging_table` RENAME TO `production_table`
  • Double-check all partitions, clustering specifications and metadata descriptions are identical between the tables

The best part is that this is a true one-liner. No chains–or headaches–involved.

To save you a headache, here are this week’s links:

If you want to read more about this method, I cover the process in more detail here.

Questions? You know where to find me: zach@pipelinetode.com.

Until next time–thanks for ingesting,

-Zach Quinn

Pipeline To DE

Top data engineering writer on Medium & Senior Data Engineer in media; I use my skills as a former journalist to demystify data science/programming concepts so beginners to professionals can target, land and excel in data-driven roles.

Read more from Pipeline To DE

Extract. Transform. Read. A newsletter from Pipeline Hi past, present or future data professional! When you apply to data analysis, data engineering or data science jobs, you likely consider factors like company name, culture and compensation. Caught up in the excitement of a fresh opportunity or compelling offer you’re neglecting an important part of your day-to-day reality in a new role: What stage of data maturity the organization is in. If you’re looking for experience building something...

Extract. Transform. Read. A newsletter from Pipeline Sponsored by Basejump AI. Don't just query your data; chat with it. Learn more. Hi past, present or future data professional! If you’re a hybrid or remote worker (or online student) you know the FOMO that sets in when you leave your desk and something at work needs your immediate attention. Even though data engineering is a role that can be done from anywhere, having to open, set up and position a laptop isn’t ideal in all situations like...

The Latest From Pipeline Hi past, present or future data professional! The opening scene of The Social Network, in which a fictionalized Mark Zuckerburg writes enough code to create a website in a single night, has forever skewed new engineers’ perceptions of development pace. If you think about it, media depictions of coding are almost always blisteringly fast and under duress. So, when you start coding in your first job, you might feel like me: Pressured to crank out as much code as quickly...